﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RIA.ServicesLibrary.Web.Services;
using System.Collections.ObjectModel;
using SL4.CustomControls;
using System.ServiceModel.DomainServices.Client;
using RIA.ServicesLibrary.Web.Models;

namespace RIA.ViewModel
{
    public class ProductListViewModel : ViewModelBase
    {

        private ProductContext _Context = null;
        public ProductListViewModel ()
        {
            _Context = new ProductContext ();
            this.DataList = new CustomPagedCollectionView<ProductSummary> ();
            this.DataList.OnRefresh += new EventHandler ( DataList_OnRefresh );
        }
        void DataList_OnRefresh ( object sender, EventArgs e )
        {
            LoadProductListExecute ( Name );
        }
        #region Declarations

        ICommand _loadProductListCommand;
        public CustomPagedCollectionView<ProductSummary> DataList { get; private set; }

        #endregion //Declarations

        #region Properties

        public int PageSize
        {
            get
            {
                return DataList.PageSize;
            }
            set
            {
                if ( value == DataList.PageSize ) return;
                DataList.PageSize = value;
                OnPropertyChanged ( "PageSize" );
            }
        }

        private string _name = "";
        public string Name
        {
            get { return _name; }
            set
            {
                this._name = value;
                OnPropertyChanged ( "Name" );
            }
        }
        #endregion //Properties

        #region Command Properties

        public ICommand LoadProductListCommand
        {
            get
            {
                if ( _loadProductListCommand == null )
                {
                    _loadProductListCommand = new DelegateCommand( param => LoadProductListExecute ( param ), param => CanLoadProductListExecute ( param ) );
                }
                return _loadProductListCommand;
            }
        }

        #endregion //Command Properties

        #region Constructors

        //TODO developers add your constructors here

        #endregion //Constructors

        #region Command Methods

        bool CanLoadProductListExecute ( object param )
        {
            return true;
        }

        void LoadProductListExecute ( object param )
        {
            string name = param.ToString ();
            ShowBusyStatus ();

            EntityQuery<ProductSummary> qry = _Context.GetProductSummaryListQuery ();
            int skip = DataList.PageIndex * DataList.PageSize;


            // Add a Where clause to filter the results
            qry = qry
                .Where ( p=>p.Name.Contains(name) )
                .OrderBy ( x =>Name )
                .Skip ( skip )
                .Take ( DataList.PageSize );
            qry.IncludeTotalCount = true;
            //Grouping List firt sort the List.


            //call back 
            LoadOperation<ProductSummary> op = _Context.Load ( qry, true );
            op.Completed += ( x, y ) =>
            {
                HideBusyStatus ();
                if ( op.HasError )
                {
                    System.Windows.MessageBox.Show ( op.Error.ToString (), "Load Error",
                    System.Windows.MessageBoxButton.OK );
                    op.MarkErrorAsHandled ();
                }
                else
                {
                    DataList.Clear ();
                    DataList.ItemCount = op.TotalEntityCount;
                    foreach ( ProductSummary c in op.Entities )
                    {
                        DataList.Add ( c );
                    }
                }

            };
        }

        #endregion //Command Methods


    }
}
